#include "cTabs.h"
#include "cGroupBox.h"
#include "cFenetre.h"
#include "cCanvas.h"
cTabs::cTabs(cHwnd * hwndParent,int x,int y,int largeur,int hauteur):
    cControl(hwndParent)
{
    //ctor
    INITCOMMONCONTROLSEX icex;
    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    icex.dwICC = ICC_TAB_CLASSES;
    InitCommonControlsEx(&icex);

    hWnd = CreateWindowEx(0,WC_TABCONTROL, "",
         WS_CHILD | WS_VISIBLE | WS_TABSTOP ,
        x, y, largeur, hauteur,
        _parent->getHwnd(), NULL, hInstance, NULL);
    /*int tailletabinfo=sizeof(TAB_INFO);
    int tailletcitem=sizeof(TCITEM);*/
    //TabCtrl_SetItemExtra(hWnd,sizeof(TAB_INFO)-sizeof(TCITEMHEADER));
    FinalInit();

}

cTabs::~cTabs()
{
    //dtor
}

bool cTabs::TraitementEvent(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,LRESULT & resultat)
{
    switch (message)
    {
        case WM_ERASEBKGND:
        {
            /*HBRUSH couleurfond=((cFenetre*)_parent)->GetBackGroundColor();
            HDC hdc=(HDC)wparam;
            RECT rect;
            GetClientRect (hWnd, &rect);

            FillRect(hdc,&rect,couleurfond);*/

            resultat=1;
            return true;
            break;
        }

        case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HBRUSH couleurfond=(HBRUSH)GetStockObject( WHITE_BRUSH );

            WNDPROC old_proc=(WNDPROC)GetProp(hWnd,"old_proc");


			HDC hdc=BeginPaint(hWnd,&ps);
			HBRUSH couleurfond2=((cFenetre*)_parent)->GetBackGroundColor();
            //HDC hdc=(HDC)wparam;
            RECT rect;
            GetClientRect (hWnd, &rect);
            rect.bottom=20;
            FillRect(hdc,&rect,couleurfond2);

            HFONT hOldFont = (HFONT)SelectObject (hdc, (HFONT)SendMessage(hwnd,WM_GETFONT,0,0));
            int nb= TabCtrl_GetItemCount(hWnd);
            RECT rectangle,corps;
            GetClientRect(hWnd,&corps);
            int curitem=TabCtrl_GetCurSel(hWnd);


            corps.top+=20;
            FillRect(hdc, &corps, couleurfond );

            SetBkColor(hdc,RGB(255,255,255));

            //DrawEdge(hdc, &corps, EDGE_RAISED, BF_FLAT |BF_RECT);

            for(int i=0;i<nb;i++)
            {
                if(i==curitem) continue;
                    SetBkColor(hdc,RGB(200,200,200));
                    TabCtrl_GetItemRect(hWnd,i,&rectangle);

                    char buffer[100]="test";
                    sprintf(buffer,onglets[i]->GetLabel().c_str());
                    if(onglets[i]->GetEnable())
                    {
                        if(itemhighted==i)
                        {
                            SetBkColor(hdc,RGB(240,240,255));
                            SetTextColor(hdc,RGB(0,0,100));
                        }
                        else
                        {
                            SetBkColor(hdc,RGB(240,240,240));
                            SetTextColor(hdc,RGB(0,0,0));
                        }

                    }

                    else
                    {
                        SetBkColor(hdc,RGB(245,245,245));
                        SetTextColor(hdc,RGB(200,200,200));
                    }
                    SetBkMode(hdc,OPAQUE);
                    HBRUSH brush;
                    int opaqueOuPas=ETO_OPAQUE;
                    if(onglets[i]->GetEnable())
                    {
                        if(itemhighted==i)
                        {
                            brush=GetSysColorBrush(COLOR_HIGHLIGHT);
                            LOGBRUSH lgbrush;
                            GetObject(GetSysColorBrush(COLOR_BTNHIGHLIGHT),sizeof(LOGBRUSH),&lgbrush);
                            COLORREF couleur1=RGB(240,240,255);

                            TRIVERTEX vertex[2] ;
                            vertex[0].x     = rectangle.left;
                            vertex[0].y     = rectangle.top;
                            vertex[0].Red   = GetRValue(couleur1)<<8;
                            vertex[0].Green = GetGValue(couleur1)<<8;
                            vertex[0].Blue  = GetBValue(couleur1)<<8;

                            couleur1=RGB(210,210,255);
                            vertex[1].x     = rectangle.right;
                            vertex[1].y     = rectangle.bottom;
                            vertex[1].Red   = GetRValue(couleur1)<<8;
                            vertex[1].Green = GetGValue(couleur1)<<8;
                            vertex[1].Blue  = GetBValue(couleur1)<<8;

                            // Create a GRADIENT_RECT structure that
                            // references the TRIVERTEX vertices.
                            GRADIENT_RECT gRect;
                            gRect.UpperLeft  = 0;
                            gRect.LowerRight = 1;

                            // Draw a shaded rectangle.
                            GradientFill(hdc, vertex, 2, &gRect, 1, GRADIENT_FILL_RECT_V);
                            SetBkMode(hdc,TRANSPARENT);
                            opaqueOuPas=0;

                        }
                        else
                            brush=GetSysColorBrush(COLOR_ACTIVEBORDER);
                    }
                    else
                    {
                        brush=GetSysColorBrush(COLOR_INACTIVEBORDER);
                    }

                    ExtTextOut(hdc,rectangle.left+6, rectangle.top+3, opaqueOuPas , &rectangle, buffer, lstrlen(buffer), 0);

                    //DrawEdge(hdc, &rectangle, EDGE_RAISED, BF_FLAT| BF_SOFT | BF_RIGHT | BF_TOP | BF_LEFT);
                    rectangle.bottom++;
                    FrameRect(hdc,&rectangle,(HBRUSH)brush);




                    //DrawCorners(hdc,&rectangle,couleurfond);
            }

            if(curitem>=0)
            {
                SetBkColor(hdc,RGB(200,200,200));
                TabCtrl_GetItemRect(hWnd,curitem,&rectangle);

                rectangle.top-=2;
                rectangle.left-=2;
                rectangle.right+=2;

                char buffer[100]="test";
                sprintf(buffer,onglets[curitem]->GetLabel().c_str());


                SetBkColor(hdc,RGB(255,255,255));
                SetTextColor(hdc,RGB(0,0,0));

                ExtTextOut(hdc,rectangle.left+6, rectangle.top+3, ETO_OPAQUE, &rectangle, buffer, lstrlen(buffer), 0);
                DrawEdge(hdc, &rectangle, EDGE_RAISED, BF_FLAT| BF_SOFT | BF_RIGHT | BF_TOP | BF_LEFT);

                int ancienRight=corps.right;
                corps.right=rectangle.left+1;
                DrawEdge(hdc, &corps, EDGE_RAISED, BF_FLAT |BF_TOP|BF_LEFT|BF_BOTTOM);

                corps.left=rectangle.right-1;
                corps.right=ancienRight;
                DrawEdge(hdc, &corps, EDGE_RAISED, BF_FLAT |BF_TOP|BF_RIGHT|BF_BOTTOM);

                corps.left=rectangle.left;
                corps.right=rectangle.right;
                DrawEdge(hdc, &corps, EDGE_RAISED, BF_FLAT |BF_BOTTOM);
            }

            SelectObject (hdc, hOldFont);
            EndPaint(hWnd,&ps);

            resultat=0;
            return true;
            break;
        }


        case WM_NCHITTEST:
        {
            TCHITTESTINFO pos = {{0, 0}, TCHT_ONITEM};
            GetCursorPos(&pos.pt);
            RECT r;
            GetWindowRect(hWnd, &r);
            pos.pt.x -= r.left;
            pos.pt.y -= r.top;
            int i = TabCtrl_HitTest(hWnd, &pos);
            if(i>=0)
            {
                this->itemhighted=i;
                if(!onglets[i]->GetEnable())
                    return HTTRANSPARENT;
            }
            else
                this->itemhighted=-1;
            break;
        }




    	case WM_NOTIFY:
    	{

            switch (((LPNMHDR)lparam)->code)
            {
                case TCN_SELCHANGING:
                {
                    ancienOnglet = TabCtrl_GetCurSel(hWnd);
                    break;
                }


            	case TCN_SELCHANGE:
                {
                    int iPage = TabCtrl_GetCurSel(hWnd);
                    int nb= TabCtrl_GetItemCount(hWnd);
                    for(int i=0;i<nb;i++)
                    {
                        if(onglets[i]->GetEnable())
                        {
                            onglets[i]->conteneur->SetVisible(i==iPage);
                            if(i==iPage)
                            {
                                /*SetWindowPos(
                                      onglets[i]->conteneur->getHwnd(),
                                      HWND_TOP,
                                      0,0,0,0,
                                      SWP_NOMOVE | SWP_NOSIZE | SWP_NOREDRAW
                                    );*/


                                onglets[i]->conteneur->getHwnd();

                            }

                        }

                        else if(i==iPage)
                        {
                            onglets[i]->conteneur->SetVisible(false);
                            onglets[ancienOnglet]->conteneur->SetVisible(true);
                            TabCtrl_SetCurSel(hWnd,ancienOnglet);
                        }

                    }
                    /*InvalidateRect(this->hWnd,NULL,true);
                    UpdateWindow(this->hWnd);*/

            		break;
                }
            };


    		break;
        }
    };

    return false;
}

cOnglet * cTabs::addTab(string libelle)
{
    int nb=TabCtrl_GetItemCount(hWnd);
    char * texte=new char[libelle.size()+1];
    strcpy(texte,libelle.c_str());
    texte[libelle.size()]=0;

    RECT dim;
    GetWindowRect(hWnd,&dim);
    cLabel * label=new cLabel(_parent,1,24,dim.right-dim.left-2,dim.bottom-dim.top-2,"");
    label->SetAncre("bottom",true,2);
    label->SetAncre("right",true,1);
    label->SetTransparent(false);


    /*TAB_INFO tabinfo;
    tabinfo.header.mask=TCIF_TEXT | TCIF_IMAGE | TCIF_PARAM;
    tabinfo.header.iImage=-1;
    tabinfo.header.pszText=texte;
    //tabinfo.lParam=(LPARAM)label;*/

    TCITEMHEADER tabinfo={0};
    tabinfo.mask=TCIF_TEXT;
    tabinfo.pszText=texte;
    tabinfo.cchTextMax=libelle.size()+1;


    cOnglet * nouvelOnglet=new cOnglet(this,nb);
    nouvelOnglet->SetEnable(true);
    nouvelOnglet->SetLabel(libelle);
    nouvelOnglet->conteneur=label;
    //tabinfo.more=nouvelOnglet;
    onglets.push_back(nouvelOnglet);

    TabCtrl_InsertItem(hWnd, nb, &tabinfo);

    delete [] texte;
    return nouvelOnglet;
}

void        cOnglet::SetEnable(bool enabled)
{
    this->enabled=enabled;
    InvalidateRect(controlParent->getHwnd(),NULL,false);
}

void        cOnglet::SetLabel(string label)
{
    this->libelle=label;
    InvalidateRect(controlParent->getHwnd(),NULL,false);


    /*TAB_INFO tabinfo;
    tabinfo.header.mask=TCIF_TEXT;
    char * nouveaulibelle=new char[label.size()];
    strcpy(nouveaulibelle,label.c_str());

    tabinfo.header.pszText=nouveaulibelle;
    tabinfo.header.cchTextMax=label.size();*/
    char * texte=new char[libelle.size()+1];
    strcpy(texte,libelle.c_str());
    texte[libelle.size()]=0;

    TCITEMHEADER tabinfo={0};
    tabinfo.mask=TCIF_TEXT;
    tabinfo.pszText=texte;
    tabinfo.cchTextMax=libelle.size()+1;

    TabCtrl_SetItem(controlParent->getHwnd(),this->tabId,&tabinfo);

    delete [] texte;

}
